package guoanmaker.provider.business.controller;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import guoanmaker.provider.business.model.PersonalCollection;
import guoanmaker.provider.business.model.PersonalUser;
import guoanmaker.provider.business.model.ProvideCommodity;
import guoanmaker.provider.business.modelnobind.PersonalCollectionUser;
import guoanmaker.provider.business.service.PersonalCollectionServiceInterface;
import guoanmaker.provider.helper.common.AjaxJson;
import guoanmaker.provider.helper.common.abstractobj.ApiController;
import io.swagger.annotations.ApiOperation;

@RestController
@RequestMapping(ApiController.COMMODITY_URL)
public class PersonalCollectionController {

	@Autowired
	private AjaxJson ajaxJson;
	
	@Autowired
	private PersonalCollectionServiceInterface personalCollectionServiceInterface;
	
	@ApiOperation(value="新增收藏商品", notes="新增收藏商品")
	@PostMapping("/insertPersonalCollection")
	public  AjaxJson insertPersonalCollection(String personalUserId,String  provideCommodityId){
		try {
			ajaxJson.setSuccess(true).setStatus(200).setData(personalCollectionServiceInterface.insertPersonalCollection( personalUserId, provideCommodityId));
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("收藏失败");
		}
		return ajaxJson;
	} 
	
	@ApiOperation(value="删除收藏商品", notes="删除收藏商品")
	@PostMapping("/deletePersonalCollection")
	public  AjaxJson deletePersonalCollection(String personalUserId,String  provideCommodityId){
		try {
			personalCollectionServiceInterface.deletePersonalCollection(personalUserId,provideCommodityId);
			ajaxJson.setSuccess(true).setStatus(200).setData("删除成功");
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("删除失败");
		}
		return ajaxJson;
	} 
	
	@ApiOperation(value="根据用户id分页查询收藏商品", notes="根据用户id分页查询收藏商品")
	@PostMapping("/selectPersonalCollectionPage")
	public  AjaxJson selectPersonalCollectionPage(String userid,Integer page,Integer size){
		try {
			Map<String,Object> map=personalCollectionServiceInterface.selectPersonalCollectionPage(userid, page, size);
			int a=(int) map.get("count");
			if(a>0){
				ajaxJson.setSuccess(true).setStatus(200).setData(map);
			}
			else{
				ajaxJson.setSuccess(true).setStatus(250).setData("没有数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
		return ajaxJson;
	} 
	@ApiOperation(value="根据用户id查询收藏商品", notes="根据用户id查询收藏商品")
	@PostMapping("/selectPersonalCollection")
	public  AjaxJson selectPersonalCollection(String userid){
		try {
			List<PersonalCollectionUser> list=personalCollectionServiceInterface.selectPersonalCollection(userid);
			if(list!=null&&list.size()>0){
				ajaxJson.setSuccess(true).setStatus(200).setData(list);
			}
			else{
				ajaxJson.setSuccess(true).setStatus(250).setData("没有数据");
			}
		} catch (Exception e) {
			e.printStackTrace();
			ajaxJson.setSuccess(true).setStatus(250).setData("查询失败");
		}
		return ajaxJson;
	} 
	
}
